<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>nginx 配置优化的几个参数 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>nginx 配置优化的几个参数</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/LNMP/">LNMP</a> <a class="article__tag-link" href="/tags/Nginx/">Nginx</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;最近在服务器上搞了一些nginx，研究了一下，总结总结。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;nginx配置文件里面需要注意的一些参数</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">worker_processes 8</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;nginx要开启的进程数 一般等于cpu的总核数 其实一般情况下开4个或8个就可以了，多了没有太多用</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;每个nginx进程消耗的内存10兆的模样</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">worker_cpu_affinity</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;仅适用于Linux，使用该选项可以绑定worker进程和CPU（2.4内核的机器用不<br>了）</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;假如是8 cpu 分配如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000</div><div class="line">00100000 01000000 10000000</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;nginx可以使用多个worker进程，原因如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">to use SMP </div><div class="line">to decrease latency when workers blockend on disk I/O </div><div class="line">to <span class="built_in">limit</span> number of connections per process when select()/poll() is</div><div class="line">used </div><div class="line">The worker_processes and worker_connections from the event sections</div><div class="line">allows you to calculate maxclients value: k</div><div class="line">max_clients = worker_processes * worker_connections</div><div class="line">worker_rlimit_nofile 102400;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;每个nginx进程打开文件描述符最大数目 配置要和系统的单进程打开文件数一致,linux 2.6内核下开启文件打开数为65535，worker_rlimit_nofile就相应应该填写65535 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;nginx调度时分配请求到进程并不是那么的均衡，假如超过会返回502错误。这里写的大一点</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">use epoll</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;nginx使用了最新的epoll（Linux 2.6内核）和kqueue（freebsd）网络I/O模型，而Apache则使用的是传统的select模型。处理大量的连接的读写，Apache所采用的select网络I/O模型非常低效。在高并发服务器中，轮询I/O是最耗时间的操作 目前Linux下能够承受高并发访问的Squid、Memcached都采用的是epoll网络I/O模型。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">worker_connections 65535;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;每个工作进程允许最大的同时连接数 （Maxclient = work_processes *　worker_connections）</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">keepalive_timeout 75</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;keepalive超时时间</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这里需要注意官方的一句话：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">The parameters can differ from each other. Line Keep-Alive:</div><div class="line">timeout=time understands Mozilla and Konqueror. MSIE itself shuts</div><div class="line">keep-alive connection approximately after 60 seconds.</div><div class="line">client_header_buffer_size 16k</div><div class="line">large_client_header_buffers 4 32k</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;客户请求头缓冲大小 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;nginx默认会用client_header_buffer_size这个buffer来读取header值，如果header过大，它会使用large_client_header_buffers来读取。如果设置过小HTTP头/Cookie过大 会报400 错误 nginx 400 bad request。求行如果超过buffer，就会报HTTP 414错误(URI Too Long)。nginx接受最长的HTTP头部大小必须比其中一个buffer大，否则就会报400的HTTP错误(Bad Request)。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">open_file_cache max 102400</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用字段:http, server, location 这个指令指定缓存是否启用,如果启用,将记录文件以下信息: 打开的文件描述符,大小信息和修改时间；存在的目录信息；在搜索文件过程中的错误信息； – 没有这个文件,无法正确读取,参考open_file_cache_errors 指令选项:</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;max - 指定缓存的最大数目,如果缓存溢出,最长使用过的文件(LRU)将被移除</p>
<p>例:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">open_file_cache max=1000 inactive=20s;</div><div class="line">open_file_cache_valid 30s; </div><div class="line">open_file_cache_min_uses 2; </div><div class="line">open_file_cache_errors on;</div><div class="line">open_file_cache_errors</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;语法:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">open_file_cache_errors on | off</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;默认值:open_file_cache_errors off 使用字段:http, server, location 这个指令指定是否在搜索一个文件是记录cache错误.</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;open_file_cache_min_uses</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;语法:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">open_file_cache_min_uses number</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;默认值:open_file_cache_min_uses 1 使用字段:http, server, location 这个指令指定了在open_file_cache指令无效的参数中一定的时间范围内可以使用的最小文件数,如 果使用更大的值,文件描述符在cache中总是打开状态.</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;open_file_cache_valid</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;语法:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">open_file_cache_valid time</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;默认值:open_file_cache_valid 60 使用字段:http, server, location 这个指令指定了何时需要检查open_file_cache中缓存项目的有效信息.</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;开启gzip</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">gzip on;</div><div class="line">gzip_min_length 1k;</div><div class="line">gzip_buffers 4 16k;</div><div class="line">gzip_http_version 1.0;</div><div class="line">gzip_comp_level 2;</div><div class="line">gzip_types text/plain application/x-JavaScript text/css</div><div class="line">application/xml;</div><div class="line">gzip_vary on;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;缓存静态文件：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">location ~* ^.+\.(swf|gif|png|jpg|js|css)$ &#123;</div><div class="line">root /usr/<span class="built_in">local</span>/ku6/ktv/show.ku6.com/;</div><div class="line">expires 1m;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;优化Linux内核参数</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line">vi /etc/sysctl.conf</div><div class="line">　　</div><div class="line"><span class="comment"># Add</span></div><div class="line">net.ipv4.tcp_max_syn_backlog = 65536</div><div class="line">net.core.netdev_max_backlog = 32768</div><div class="line">net.core.somaxconn = 32768</div><div class="line">net.core.wmem_default = 8388608</div><div class="line">net.core.rmem_default = 8388608</div><div class="line">net.core.rmem_max = 16777216</div><div class="line">net.core.wmem_max = 16777216</div><div class="line">net.ipv4.tcp_timestamps = 0</div><div class="line">net.ipv4.tcp_synack_retries = 2</div><div class="line">net.ipv4.tcp_syn_retries = 2</div><div class="line">net.ipv4.tcp_tw_recycle = 1</div><div class="line"><span class="comment">#net.ipv4.tcp_tw_len = 1</span></div><div class="line">net.ipv4.tcp_tw_reuse = 1</div><div class="line">net.ipv4.tcp_mem = 94500000 915000000 927000000</div><div class="line">net.ipv4.tcp_max_orphans = 3276800</div><div class="line"><span class="comment">#net.ipv4.tcp_fin_timeout = 30</span></div><div class="line"><span class="comment">#net.ipv4.tcp_keepalive_time = 120</span></div><div class="line">net.ipv4.ip_local_port_range = 1024 65535</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;附录：一些错误排查</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;PHP-cgi进程数不够用、php执行时间长（MySQL慢）、或者是php-cgi进程死掉，都会出现502错误</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;一般来说Nginx 502 Bad Gateway和php-fpm.conf的设置有关，而Nginx 504 Gateway Time-out则是与nginx.conf的设置有关</p>
<ol>
<li>查看当前的PHP FastCGI进程数是否够用：</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">netstat -anpo | grep <span class="string">"php-cgi"</span> | wc -l</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果实际使用的“FastCGI进程数”接近预设的“FastCGI进程数”，那么，说明“FastCGI进程数”不够用，需要增大。</p>
<ol>
<li>部分PHP程序的执行时间超过了Nginx的等待时间，可以适当增加nginx.conf配置文件中FastCGI的timeout时间，例如：</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">http </div><div class="line">&#123;</div><div class="line">......</div><div class="line">fastcgi_connect_timeout 300;</div><div class="line">fastcgi_send_timeout 300;</div><div class="line">fastcgi_read_timeout 300;</div><div class="line">......</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;413 Request Entity Too Large</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;增大client_max_body_size</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;client_max_body_size:指令指定允许客户端连接的最大请求实体大小,它出现在请求头部的Content-Length字段. 如果请求大于指定的值,客户端将收到一个”Request Entity Too Large” (413)错误. 记住,浏览器并不知道怎样显示这个错误.</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;php.ini中增大post_max_size 和upload_max_filesize</p>

	

	
		<span class="different-posts"><a href="/2017/10/12/Nginx/9. nginx 配置优化的几个参数/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
